
#include "main.h"

/* Alarms */
#define   ALARM_ON_FALL				0x00
#define   ALARM_ON_RISE				0x01
#define   ALARM_ON_ANY_CHANGE		0xFF
#define   RESET_ALL_HANDLERS		0xFF
	
INPUTS_DESCRIPTOR InputDescriptor;

//unsigned char InpEvents[pInpEvents];

typedef struct
{
	unsigned	RESET		:		1;			
	unsigned	H1	 		:		1;			
	unsigned	H2	 		:		1;			
	unsigned	H3	 		:		1;			
	unsigned	H4	 		:		1;			
	unsigned	H5	 		:		1;			
	unsigned	H6	 		:		1;			
	unsigned	H7	 		:		1;			
	unsigned	H8	 		:		1;			
	unsigned	H9	 		:		1;			
	unsigned	H10	 	:		1;			
	unsigned	H11 		:		1;			
	unsigned	H12 		:		1;			
	unsigned	H13	 	:		1;			
	unsigned	H14	 	:		1;			
	unsigned	H15	 	:		1;			
		
}HANDLER;

typedef union 
{
		unsigned int Handlers;
		HANDLER Handler;
}CALL_HANDLERS;

CALL_HANDLERS CallHandlers;

unsigned char ListNumber;

signed char get_group_number ( char *m )
{
	unsigned char* p = FindCharNumber(m, '[', 1);
	
	if ( p ) 
	{
		return StrToLong(p);
	}

	return -1;
}

//typedef struct
//{
//		unsigned char InputName;  		/* Input number */
//		unsigned char EventsOnEdge;/* 'RISE'(1), 'FALL'(0), 'PULSE'(EventsOnEdge > 1), 'ANY CHANGE'(EventsOnEdge==0xFF)*/
//		unsigned long Reserve;
//		unsigned char Message[INPUTS_DESCRIPTOR_MESSAGE_SIZE+1];
//		
//}INPUTS_DESCRIPTOR;				  /* Input event descriptor */



void InpHandler_1( void )
{
	//static unsigned char State = 0, Timer, Delta;
	//INPUTS_DESCRIPTOR My_InputDescriptor;

	putsf(">>I_1\r", AUX_PORT);
	CallHandlers.Handler.H1 = 0; /* not call me */
	

/* 	switch ( State ) */
/* 	{ */
/* 		case 0:	//InputDescriptor.EventsOnEdge == 0xFF - ANY CHANGE on the Input is ALARM; */
/* 			if ( InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge ) */
/* 			{ */
/* 				OUT_1 ^= 1; */
/* 				State +=2; */
/* 			}	 */
/* 			else */
/* 				if ( InputDescriptor.EventsOnEdge > 1 ) /\* PULSE case *\/ */
/* 				{ */
/* 					//wait for PULSE END */
/* 					Timer = InputDescriptor.EventsOnEdge; /\* Get duration of PULSE *\/ */
/* 					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5%  */
/* 					Timer += Delta; */
/* 				  	State++; */
/* 				} */
/* 				else */
/* 				{ */
/* 					CallHandlers.Handler.H1 = 0; /\* not call me *\/ */
/* 				} */
/* 			break; */

/* 		case 1: */
/* 			if ( (InputsEvent & 0x0F) == 1 )	/\* if EVENTS on my inputs *\/ */
/* 			{ */
/* 				if ( Timer > Delta<<1 ) /\* if pulse duration less than (PULSE - Delta) *\/ */
/* 				{ */
/* 					State = 0; */
/* 					CallHandlers.Handler.H1 = 0; /\* not call me *\/ */
/* 				} */
/* 			} */
/* 			else */
/* 				if ( Time_10ms && --Timer == 0 ) */
/* 				{ */
/* 					State = 0; */
/* 					CallHandlers.Handler.H1 = 0; /\* not call me *\/ */
/* 				} */
/* 			break; */

/* 		case 2: */
/* 			if ( SMS_Sender_Manager__Status == READY  ) */
/* 			{ */
/* 				//SMS_Sender_Manager__Status = BUSY; */
/* 				SMS_Sender_Manager__SendTo = LIST_OWNERS; */
/* 				My_InputDescriptor.InputName = 1; */
/* 				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR ) */
/* 				{ */
/* 					Debug(">>ERROR: GET LIST_INPUTS_DESCRIPTORS", My_InputDescriptor.InputName); */
/* 				} */
/* 				else */
/* 				{ */
/* 					StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message);   */
/* 				} */
/* 				Tools = SMS_Sender_Manager; */
/* 				State = 0; */
/* 				CallHandlers.Handler.H1 = 0; /\* stop call the Handler *\/ */
/* 			} */
/* 			break; */

/* 		default: */
/* 			; */
/* 	} */
}

void InpHandler_2( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	//putsf(">>I_2\r", MODEM_PORT);
	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_2\r", AUX_PORT);
		State = 0;
		return ;
	}
	
	switch ( State )
	{
		case 0:
			if ( InputsEvent == 0 )
				return ;
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_1 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H2 = 0; /* not call me */
				}
			break;

		case 1:		/* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 2 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H2 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H2 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 2;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));

				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H2 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}


/* 			if ( SMS_Sender_Manager__Status == READY  ) */
/* 			{ */
/* 				SMS_Sender_Manager__Status = BUSY; */
/* 				SMS_Sender_Manager__SendTo = LIST_OWNERS; */
/* 				My_InputDescriptor.InputName = 2; */
/* 				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR ) */
/* 				{ */
/* 					Debug(">> ERROR: GET LIST_INPUTS_DESCRIPTORS INP: ", My_InputDescriptor.InputName); */
/* 				} */
/* 				else */
/* 				{ */
/* 					StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[0]);   */
/* 					Debug(">> GET LIST_INPUTS_DESCRIPTORS == OK INP: ", My_InputDescriptor.InputName); */
/* 					Tools = SMS_Sender_Manager; */
/* 				} */
/* 				State = 0; */
/* 				CallHandlers.Handler.H2 = 0; /\* stop call the Handler *\/ */
/* 			} */
/* 			else */
/* 			{ */
/* 				Debug(">>Wait for SMS_Sender_Manager__Status\r", 0); */
/* 			} */
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H2 = 0; /* stop call the Handler */
			break;


		default:
			;
	}
	
}

void InpHandler_3( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_3\r", AUX_PORT);
		State = 0;
		return ;
	}
	
	//dbg("InpHandler_3\n"); //Dima

	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_2 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H3 = 0; /* not call me */
				}
			break;

		case 1:		/* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 3 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H3 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H3 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY )
			{
//				SMS_Sender_Manager__Status = BUSY;
//				SMS_Sender_Manager__SendTo = LIST_OWNERS;
				
				My_InputDescriptor.InputName = 3;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d\n", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

/* 				else */
/* 				{ */
/* 					StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[0]);   */
/* 				} */


				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));


				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d\n", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H3 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H3 = 0; /* stop call the Handler */
			break;


		default:
			;
	}
}

void InpHandler_4( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_4\r", AUX_PORT);
		State = 0;
		return ;
	}

	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_3 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H4 = 0; /* not call me */
				}
			break;

		case 1:		/* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 4 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H4 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H4 = 0; /* not call me */
				}
			break;

		case 2: 
			//Dima if ( SMS_Sender_Manager__Status == READY  )
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 4;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}


				Debug("\n @ My_InputDescriptor.Message[MessageNumber]: %s\n", My_InputDescriptor.Message[MessageNumber]);
				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));

				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H4 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:               
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H4 = 0; /* stop call the Handler */
			break;



/* 			if ( SMS_Sender_Manager__Status == READY  ) */
/* 			{ */
/* 				SMS_Sender_Manager__Status = BUSY; */
/* 				SMS_Sender_Manager__SendTo = LIST_OWNERS; */
/* 				My_InputDescriptor.InputName = 4; */
/* 				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR ) */
/* 				{ */
/* 					Debug(">>ERROR: GET LIST_INPUTS_DESCRIPTORS", My_InputDescriptor.InputName); */
/* 				} */
/* 				else */
/* 				{ */
/* 					StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[0]);   */
/* 				} */
/* 				Tools = SMS_Sender_Manager; */
/* 				State = 0; */
/* 				CallHandlers.Handler.H4 = 0; /\* stop call the Handler *\/ */
/* 			} */
			//break;

		default:
			;
	}
}

void InpHandler_5( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_5\r", AUX_PORT);
		State = 0;
		return ;
	}

	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_4 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H5 = 0; /* not call me */
				}
			break;

		case 1:						  /* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 5 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H5 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H5 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 5;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));


				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H5 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H5 = 0; /* stop call the Handler */
			break;

		default:
			;
	}
}

void InpHandler_6 ( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_6\r", AUX_PORT);
		State = 0;
		return ;
	}

	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_4 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H6 = 0; /* not call me */
				}
			break;

		case 1:						  /* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 6 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H6 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H6 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 6;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));


				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H6 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H6 = 0; /* stop call the Handler */
			break;

		default:
			;
	}
}

void InpHandler_7( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_7\r", AUX_PORT);
		State = 0;
		return ;
	}

	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_4 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H7 = 0; /* not call me */
				}
			break;

		case 1:						  /* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 7 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H7 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H7 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 7;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));


				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H7 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H7 = 0; /* stop call the Handler */
			break;

		default:
			;
	}
}

void InpHandler_8( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_8\r", AUX_PORT);
		State = 0;
		return ;
	}

	//dbg("InpHandler_8"); //Dima
	
	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_4 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H8 = 0; /* not call me */
				}
			break;

		case 1:						  /* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 8 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H8 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H8 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 8;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));


				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H8 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H8 = 0; /* stop call the Handler */
			break;

		default:
			;
	}
}

void InpHandler_9( void )
{
	static unsigned char State = 0, Timer, Delta, MessageNumber = 0;
	INPUTS_DESCRIPTOR My_InputDescriptor;

	
	if ( CallHandlers.Handler.RESET )
	{
		putsf(">>I_9\r", AUX_PORT);
		State = 0;
		return ;
	}

	switch ( State )
	{
		case 0:
			if (  InputDescriptor.EventsOnEdge == 0xFF || (InputsEvent & 0x80)>>7 == InputDescriptor.EventsOnEdge )
			{
				OUT_4 = 10;
				State +=2;
			}	
			else
				if ( InputDescriptor.EventsOnEdge > 1 ) /* PULSE case */
				{
					//wait for PULSE END
					Timer = InputDescriptor.EventsOnEdge; /* Get duration of PULSE */
					Delta = InputDescriptor.EventsOnEdge >> 3; // 100% : 8 == 12.5% 
					Timer += Delta;
				  	State++;
				}
				else
				{
					CallHandlers.Handler.H9 = 0; /* not call me */
				}
			break;

		case 1:						  /* Wait for end of PULSE */
			if ( (InputsEvent & 0x0F) == 9 )	/* if EVENTS on my inputs */
			{
				if ( Timer > Delta<<1 ) /* if pulse duration less than (PULSE - Delta) */
				{
					State = 0;
					CallHandlers.Handler.H9 = 0; /* not call me */
				}
			}
			else
				if ( Time_10ms && --Timer == 0 )
				{
					State = 0;
					CallHandlers.Handler.H9 = 0; /* not call me */
				}
			break;

		case 2:
			if ( SMS_Sender_Manager__Status != BUSY  )
			{
				My_InputDescriptor.InputName = 9;
				if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&My_InputDescriptor) == EX_ERROR )
				{
					Debug(">>ERROR - GET LIST_INPUTS_DESCRIPTORS %d", My_InputDescriptor.InputName);
					State = 3;
					return ;
				}

				SMS_Sender_Manager__SendTo = ListNumber_ToAddress(
					get_group_number(My_InputDescriptor.Message[MessageNumber]));


				if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
				{
					Debug(">>ERROR - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
					State = 3;
					return ;
				}
				
				//StrCpy(SMS_Sender_Manager__Message, My_InputDescriptor.Message[MessageNumber]);
				Write_SMS(My_InputDescriptor.Message[MessageNumber]);

				if ( ++MessageNumber > INPUTS_DESCRIPTOR_MESSAGES_COUNT )
				{
					//MessageNumber = 0;
					State = 3;
					//CallHandlers.Handler.H5 = 0; /* stop call the Handler */
				}
				else
				{
					SMS_Sender_Manager__Status = BUSY;
					Tools = SMS_Sender_Manager;
				}
			}
			break;

		case 3:
			MessageNumber = 0;
			State = 0;
			CallHandlers.Handler.H9 = 0; /* stop call the Handler */
			break;

		default:
			;
	}
}


void InputsManager ( void )
{
	static unsigned char State = 0;

	//if( Time_1000ms ) putsf("\rInp", DEBUG_PORT);
/* 	if( Time_1000ms )  */
/* 		Debug("InputsEvent", InputsEvent ); */


	if ( InputsEvent )
	{
		Debug("InputsEvent: 0x%X\n", InputsEvent);
		Debug("State: %d\n", State);

		switch ( State )
		{
			case 0:						  /* wait for RESET (active(work) mode) */
				if ( InputsEvent == INPUT_1_UP )	/* disable all processing of the inputs + reset all handlers */
				{
					CallHandlers.Handlers = RESET_ALL_HANDLERS;
					State++;
				} 
				else
				{
					InputDescriptor.InputName = InputsEvent & 0x0F;
					/* Get Inputs descriptor */
					if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&InputDescriptor) == EX_ERROR )
					{
						Debug("NOT FOUND DESCRIPTOR OF INP: %d\n", InputDescriptor.InputName); 
						return;
					}

					/* Set bit of Handlers call */
					CallHandlers.Handlers |= (unsigned int)1 << InputDescriptor.InputName;
					Debug("DESCRIPTOR == OK OF INP: %d\n", InputDescriptor.InputName);
					Debug("CallHandlers.Handlers == 0x%X\n", CallHandlers.Handlers);
				}
				break;

			case 1:						  /* wait for start ( not active mode) */
				if ( InputsEvent == INPUT_1_DOWN )	/* start of processing of the inputs */
				{
					State--;
				}

				return ;

			default :
				State = 0;
		}
	}
	else
		if( State != 0 )
			return ;
	

	
	if ( CallHandlers.Handler.H2 ) /* PANIC BUTTON INPUT - IT'S WORKING ALWAYS */
		InpHandler_2();

	//if ( SysState == STATE_ARM ) 
	{
		if ( CallHandlers.Handler.H3 )
			InpHandler_3();

		if ( CallHandlers.Handler.H4 )
			InpHandler_4();

		if ( CallHandlers.Handler.H5 )
			InpHandler_5();

		if ( CallHandlers.Handler.H6 )
			InpHandler_6();

		if ( CallHandlers.Handler.H7 )
			InpHandler_7();

		if ( CallHandlers.Handler.H8 )
			InpHandler_8();

		if ( CallHandlers.Handler.H9 )
			InpHandler_9();
	

		if ( CallHandlers.Handlers == RESET_ALL_HANDLERS )
		{
			CallHandlers.Handlers = 0;
		}
	}
}
 

	

/* 	if ( InputsEvent != INPUT_1_UP ) */
/* 	{ */
/* 		if ( InputsState.INP_1 == LOW ) */
/* 		{ */
/* 			InputName = InputsEvent & 0x0F; */
/* 			InputDescriptor.InputName = InputName; */

/* 			/\* Get Inputs descriptor *\/ */
/* 			if ( Ex_Entry_Read(LIST_INPUTS_DESCRIPTORS, (unsigned char*)&InputDescriptor) == EX_ERROR ) */
/* 			{ */
/* 				Debug("NOT FOUND DESCRIPTOR OF INP: ", InputDescriptor.InputName);  */
/* 				return; */
/* 			} */

/* 			/\* Set bit of Handlers call *\/ */
/* 			CallHandlers.Handlers |= (unsigned char)1 << InputName; */
/* 			Debug("DESCRIPTOR == OK OF INP: ", InputDescriptor.InputName); */
/* 			Debug("CallHandlers.Handlers == ", CallHandlers.Handlers); */
/* 		} */
/* 	} */
/* 	else */
/* 	{ */
/* 		// Reset all handlers */
/* 		CallHandlers.Handlers = RESET_ALL_HANDLERS; */
/* 	} */



/* 			switch ( InputDescriptor.EventsOnEdge ) */
/* 			{ */
/* 				case ALARM_ON_FALL: */
/* 					if ( !(InputsEvent & 0x80) )  */
/* 					{ */
/* 						OUT_1 = LOW;	/\* switch ON of LED1 *\/ */
/* 						State++; */
/* 					} */
/* 					else */
/* 						State = 0; */
/* 					break; */

/* 				case ALARM_ON_RISE: */
/* 					if ( InputsEvent & 0x80 )  */
/* 					{ */
/* 						OUT_1 = LOW;	/\* switch ON of LED1 *\/ */
/* 						State++; */
/* 					} */
/* 					else */
/* 						State = 0; */
/* 					break; */

/* 				case ALARM_ON_ANY_CHANGE: */
/* 					State++; */
/* 					break; */

/* 				default:				    */
/* 					//ALARM on PULSE */
/* 					OUT_1 = OUT_BLINK; */
/* 			} */


/* 	switch ( InputsEvent & 0x0F ) */
/* 	{ */
/* 		case 0: */

/* 			break; */

/* 		case 1: */
/* 			InpHandler_1(); */
/* 			break; */

/* 		case 2: */
/* 			InpHandler_2(); */
/* 			break; */

/* 		case 3: */
/* 			InpHandler_3(); */
/* 			break; */

/* 		case 4: */
/* 			InpHandler_4(); */
/* 			break; */

/* 		case 5: */
/* 			InpHandler_5(); */
/* 			break; */

/* 		default : */
/* 			; */
/* 	} */

/* void InputsHandlers_Init( void ) */
/* { */
/* //	Handlers_List.List = 0; */

/* /\* 	HandlersList[0].Call = InpHandler_Global; *\/ */
/* /\* 	HandlersList[0].Caller = NULL; *\/ */

/* /\* 	HandlersList[1].Call = InpHandler_1; *\/ */
/* /\* 	HandlersList[1].Caller = NULL; *\/ */
	
/* /\* 	HandlersList[2].Call = InpHandler_2; *\/ */
/* /\* 	HandlersList[2].Caller = NULL; *\/ */
	
/* /\* 	HandlersList[3].Call = InpHandler_3; *\/ */
/* /\* 	HandlersList[3].Caller = NULL; *\/ */

/* /\* 	HandlersList[4].Call = InpHandler_4; *\/ */
/* /\* 	HandlersList[4].Caller = NULL; *\/ */

/* /\* 	HandlersList[5].Call = InpHandler_5; *\/ */
/* /\* 	HandlersList[5].Caller = NULL; *\/ */
/* } */


 

/* void SysEvent_Add ( unsigned char Event ) */
/* { */
/* 	EventsList[pEventsList] = Event; */
/* 	if ( ++pEventsList == EVENTS_MAX ) */
/* 	{ */
/* 		pEventsList = 0; */
/* 	} */
/* } */
/* //======================= */

/* void EventsManager( void ) */
/* { */
/* 	unsigned char i, Max, j = 0, Format[EVENTS_MAX]; */


/* 	if ( pEventsList == 0 ) */
/* 		return; */

/* 	MemCpy(Format,EventsList, pEventsList);  */
/* 	ClearBuffer(EventsList, EVENTS_MAX); */
/* 	Max = pEventsList; */
/* 	for( i = 0; i < Max; i++ ) */
/* 	{ */
/* 		if ( Format[i] == 0 ) */
/* 		{ */
/* 			pEventsList--; */
/* 		} */
/* 		else */
/* 		{ */
/* 			EventsList[j++] = Format[i]; */
/* 		} */
/* 	} */

/* 	if ( pEventsList ) */
/* 	{ */
/* 		for ( CurrentEvent_Index = 0; CurrentEvent_Index < pEventsList; CurrentEvent_Index++) */
/* 		{ */
/* 			CurrentEvent = EventsList[CurrentEvent_Index]; */
/* 			CurrentEvent &= 0x0F; */
/* 			HandlersList[ CurrentEvent & 0x0F].Call();			 */
/* 		} */
/* 	} */
/* } */



